package com.ravi.service;

import com.ravi.core.patient.BasePatient;
import com.ravi.core.patient.NormalPatient;
import com.ravi.core.patient.VipPatient;
import com.ravi.core.queue.CheckUpQueue;
import com.ravi.core.queue.HashQueueManager;
import com.ravi.dao.CheckGroupDAO;
import com.ravi.dao.CheckPlanDAO;
import com.ravi.dao.CheckRoomDAO;
import com.ravi.dao.PatientQueueInfoDAO;
import com.ravi.entity.CheckGroup;
import com.ravi.entity.CheckRoom;
import com.ravi.entity.PatientQueueInfo;
import com.ravi.exception.CheckSystemException;
import com.ravi.exception.ErrorCode;
import com.ravi.vo.PatientGroupVo;
import com.ravi.vo.PlanGroupVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

@Service
@Slf4j
public class QueueService {

	@Autowired
	private CheckRoomDAO checkRoomDAO;

	@Autowired
	private CheckGroupDAO checkGroupDAO;

	@Autowired
	private CheckPlanDAO checkPlanDAO;

	@Autowired
	private PatientQueueInfoDAO patientQueueInfoDAO;

	public boolean needFirstSign(Integer patientId) {
		List<PatientGroupVo> patientGroupVoList = checkGroupDAO.selectPatientGroupRecordsByPId(patientId);
		// 空记录则表明还未进行第一次签到
		return patientGroupVoList.isEmpty();
	}

	public void firstSign(Integer patientId) {
		PatientQueueInfo patientInfo = patientQueueInfoDAO.selectPatientQueueInfoByPatientId(patientId);
		if (ObjectUtils.isEmpty(patientInfo)) {
			throw new CheckSystemException("找不到此用户", ErrorCode.PATIENT_NOT_FOUND);
		}
		// 用户合法则查询PlanGroup的记录
		PlanGroupVo planGroupVo = checkPlanDAO.selectPlanGroupVoByPlanId(patientInfo.getPlanId());
		List<PatientGroupVo> list = new CopyOnWriteArrayList<>();
		for (CheckGroup checkGroup : planGroupVo.getCheckGroupList()) {
			list.add(new PatientGroupVo(patientInfo.getPatientId(), checkGroup.getGroupId(), 0));
		}
		// 保证了不会重复插入 patientId-groupId 这样的记录
		int batch = checkGroupDAO.insertPatientGroupBatch(list);

		log.info("QueueService--firstSign: " + batch + "条记录被打印!");
	}

	public boolean queueInfoEmpty(Integer queueId) {
		CheckRoom room = checkRoomDAO.selectCheckRoomById(queueId);
		return ObjectUtils.isEmpty(room);
	}

	public PatientQueueInfo enqueueValid(Integer patientId) {
		PatientQueueInfo patientInfo = patientQueueInfoDAO.selectPatientQueueInfoByPatientId(patientId);
		if (ObjectUtils.isEmpty(patientInfo)) {
			throw new CheckSystemException("找不到此用户", ErrorCode.PATIENT_NOT_FOUND);
		}
		// TODO:是否有项目

		// TODO:是否已经完成项目

		// TODO: 是否已经入队

		CheckUpQueue checkUpQueue = HashQueueManager.getQueue(patientInfo.getQueueId());
		if (checkUpQueue.isContain(new NormalPatient(patientInfo))) {
			throw new CheckSystemException("该用户已经入队！请勿重复入队！", ErrorCode.ENQUEUE_AGAIN);
		}
		return patientInfo;
	}

	/**
	 *
	 * @param patientInfo 不允许为非法id
	 */
	public void enqueue(PatientQueueInfo patientInfo) {
		// 根据级别创建存放于内存的派生体检用户对象
		BasePatient patient = null;
		if (patientInfo.getLevel() == 1) {
			patient = new VipPatient(patientInfo);
		}else {
			patient = new NormalPatient(patientInfo);
		}
		// 入队
		CheckUpQueue queue = HashQueueManager.getQueue(patientInfo.getQueueId());
		queue.add(patient);
	}
}
